package com.chenhy.server.utils;

import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;


public class FastDFSUtils {

    private static final Logger logger = LoggerFactory.getLogger(FastDFSUtils.class);

    // 初始化工具类(客户端)
    static {
        try {
            // 获取配置文件的相对路径
            String path = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();
            // 使用配置文件初始化客户端
            ClientGlobal.init(path);
        } catch (Exception e) {
            logger.error("初始化FastDFS工具类失败", e.getMessage());
        }
    }

    // 通过tracker客户端拿到tracker服务器
    private static TrackerServer getTrackerServer() throws IOException {
        TrackerClient trackerClient = new TrackerClient();
        return trackerClient.getTrackerServer();
    }


    // 通过Tracker服务器拿到Storage客户端
    private static StorageClient getStorageClient() throws IOException {
        TrackerServer trackerServer = getTrackerServer();
         return new StorageClient(trackerServer, null);
    }

    // 上传文件
    // 字符串数组中有两个值，groupName和上传之后的文件名
    public static String[] upload(MultipartFile file){
        String filename = file.getOriginalFilename();
        logger.info("文件名", filename);

        StorageClient storageClient = null;

        String[] uploadResult = null;

        try {
            // 获取Storage客户端
            storageClient = getStorageClient();
            // 上传文件，传入文件的字节码，文件后缀名以及文件的描述
            uploadResult = storageClient.upload_file(file.getBytes(), filename.substring(filename.lastIndexOf(".") + 1), null);
        } catch (Exception e) {
            logger.error("上传文件失败", e.getMessage());
        }

        if (uploadResult == null){
            logger.error("上传失败", storageClient.getErrorCode());
        }

        return uploadResult;
    }


    // 获取FastDFS中文件的文件信息
    public static FileInfo getFileInfo(String groupName, String remoteFileName){
        // 获取Storage客户端
        StorageClient storageClient = null;

        try {
            storageClient = getStorageClient();
            return storageClient.get_file_info(groupName, remoteFileName);
        } catch (Exception e) {
            logger.error("文件信息获取失败", e.getMessage());
        }

        return null;
    }

    // 下载文件
    public static InputStream downFile(String groupName, String remoteFileName){
        StorageClient storageClient = null;
        try {
            storageClient = getStorageClient();
            byte[] fileByte = storageClient.download_file(groupName, remoteFileName);
            return new ByteArrayInputStream(fileByte);
        } catch (Exception e) {
            logger.error("文件下载失败", e.getMessage());
        }
        return null;
    }

    public static void deleteFile(String groupName, String remoteFileName){
        StorageClient storageClient = null;
        try {
            storageClient = getStorageClient();
            storageClient.delete_file(groupName, remoteFileName);
        } catch (Exception e) {
            logger.error("文件删除失败", e.getMessage());
        }
    }

    // 获取tracker服务器地址
    public static String getTrackerUrl(){
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = null;
        StorageServer storeStorage = null;
        try {
            trackerServer = trackerClient.getTrackerServer();
            storeStorage = trackerClient.getStoreStorage(trackerServer);
        } catch (Exception e) {
            logger.error("文件路径获取失败", e.getMessage());
        }
        return "http://"+storeStorage.getInetSocketAddress().getHostString()+":8888";
    }




}
